home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / ld / dist / patch / ld.c.rej < prev    next >
Encoding:
Text File  |  1990-10-26  |  62.4 KB  |  2,338 lines

  1. ***************
  2. *** 23,37 ****
  3.   #include <sys/types.h>
  4.   #include <sys/stat.h>
  5.   #include <sys/file.h>
  6.   #ifndef sony_news
  7.   #include <fcntl.h>
  8.   #endif
  9.   
  10.   #ifdef COFF_ENCAPSULATE
  11.   #include "a.out.encap.h"
  12.   #else
  13.   #include <a.out.h>
  14.   #endif
  15.   
  16.   #ifndef N_SET_MAGIC
  17.   #define N_SET_MAGIC(exec, val)  ((exec).a_magic = val)
  18. --- 23,60 ----
  19.   #include <sys/types.h>
  20.   #include <sys/stat.h>
  21.   #include <sys/file.h>
  22. + #ifndef USG
  23. + #include <sys/time.h>
  24. + #include <sys/resource.h>
  25. + #endif
  26.   #ifndef sony_news
  27.   #include <fcntl.h>
  28.   #endif
  29.   
  30. + #if !defined(A_OUT) && !defined(MACH_O)
  31. + #define A_OUT
  32. + #endif
  33. + #ifdef A_OUT
  34.   #ifdef COFF_ENCAPSULATE
  35.   #include "a.out.encap.h"
  36.   #else
  37.   #include <a.out.h>
  38.   #endif
  39. + #endif
  40. + #ifdef MACH_O
  41. + #ifndef A_OUT
  42. + #include <nlist.h>
  43. + #include <reloc.h>
  44. + #endif
  45. + #ifndef N_TEXT
  46. + #define N_TEXT 0x04
  47. + #define N_DATA 0x06
  48. + #define N_BSS 0x08
  49. + #endif
  50. + #include <sys/loader.h>
  51. + #endif
  52.   
  53.   #ifndef N_SET_MAGIC
  54.   #define N_SET_MAGIC(exec, val)  ((exec).a_magic = val)
  55. ***************
  56. *** 219,235 ****
  57.   /* Define this to specify the default executable format.  */
  58.   
  59.   #ifdef hpux
  60. ! #define DEFAULT_MAGIC NMAGIC  /* hpux bugs screw ZMAGIC */
  61.   #endif
  62.   
  63. - #ifndef DEFAULT_MAGIC
  64. - #define DEFAULT_MAGIC ZMAGIC
  65. - #endif
  66.   /* Ordinary 4.3bsd lacks these macros in a.out.h.  */
  67.   
  68.   #ifndef N_TXTADDR
  69. ! #if defined(vax) || defined(sony_news)
  70.   #define N_TXTADDR(X) 0
  71.   #endif
  72.   #ifdef is68k
  73. --- 250,262 ----
  74.   /* Define this to specify the default executable format.  */
  75.   
  76.   #ifdef hpux
  77. ! #define DEFAULT_OUTPUT_STYLE OUTPUT_READONLY_TEXT
  78.   #endif
  79.   
  80.   /* Ordinary 4.3bsd lacks these macros in a.out.h.  */
  81.   
  82.   #ifndef N_TXTADDR
  83. ! #if defined(vax) || defined(sony_news) || defined(hp300) || defined(pyr)
  84.   #define N_TXTADDR(X) 0
  85.   #endif
  86.   #ifdef is68k
  87. ***************
  88. *** 238,247 ****
  89.   #ifdef sequent
  90.   #define    N_TXTADDR(x) (N_ADDRADJ(x))
  91.   #endif
  92.   #endif
  93.   
  94.   #ifndef N_DATADDR
  95. ! #if defined(vax) || defined(sony_news)
  96.   #define N_DATADDR(x) \
  97.       (((x).a_magic==OMAGIC)? (N_TXTADDR(x)+(x).a_text) \
  98.       : (page_size+((N_TXTADDR(x)+(x).a_text-1) & ~(page_size-1))))
  99. --- 265,277 ----
  100.   #ifdef sequent
  101.   #define    N_TXTADDR(x) (N_ADDRADJ(x))
  102.   #endif
  103. + #ifdef NeXT
  104. + #define N_TXTADDR(X) ((X).a_magic == ZMAGIC ? page_size : 0)
  105. + #endif
  106.   #endif
  107.   
  108.   #ifndef N_DATADDR
  109. ! #if defined(vax) || defined(sony_news) || defined(hp300) || defined(pyr)
  110.   #define N_DATADDR(x) \
  111.       (((x).a_magic==OMAGIC)? (N_TXTADDR(x)+(x).a_text) \
  112.       : (page_size+((N_TXTADDR(x)+(x).a_text-1) & ~(page_size-1))))
  113. ***************
  114. *** 264,277 ****
  115.   #ifdef sparc
  116.   #define INITIALIZE_HEADER \
  117.     {outheader.a_machtype = M_SPARC; outheader.a_toolversion = 1;}
  118. ! #endif
  119. ! #if defined(mc68010) || defined(m68010)
  120. ! #define INITIALIZE_HEADER outheader.a_machtype = M_68010
  121. ! #endif
  122. ! #ifndef INITIALIZE_HEADER
  123. ! #define INITIALIZE_HEADER outheader.a_machtype = M_68020
  124. ! #endif
  125. ! #endif
  126.   #ifdef ALTOS
  127.   #define INITIALIZE_HEADER N_SET_MACHTYPE (outheader, M_68020)
  128.   #endif
  129. --- 306,328 ----
  130.   #ifdef sparc
  131.   #define INITIALIZE_HEADER \
  132.     {outheader.a_machtype = M_SPARC; outheader.a_toolversion = 1;}
  133. ! #endif /* sparc.  */
  134. ! #if defined(mc68000)
  135. ! /* Set the machine type according to the machine type of the .o files.
  136. !    If they are all sun2 (68010), then the type of the executable is sun2.
  137. !    If any is sun3 (68020), then the type of the executable is sun3.
  138. !    This is consistent with the Sun loader and more useful than having
  139. !    it depend on which machine you are on when you run ld.  */
  140. ! static int sun_machtype = M_68010;
  141. ! #define INITIALIZE_HEADER outheader.a_machtype = sun_machtype
  142. ! #define READ_HEADER_HOOK(machtype) \
  143. !   if (machtype == M_68020)           \
  144. !     {                     \
  145. !       sun_machtype = M_68020;         \
  146. !     }
  147. ! #endif /* mc68000.  */
  148. ! #endif /* Sun.  */
  149.   #ifdef ALTOS
  150.   #define INITIALIZE_HEADER N_SET_MACHTYPE (outheader, M_68020)
  151.   #endif
  152. ***************
  153. *** 288,294 ****
  154.   #endif
  155.   #if defined(i386) && !defined(sequent)
  156.   #define INITIALIZE_HEADER N_SET_MACHTYPE (outheader, M_386)
  157. ! #endif
  158.   
  159.   #ifdef is68k
  160.   /* This enables code to take care of an ugly hack in the ISI OS.
  161. --- 339,351 ----
  162.   #endif
  163.   #if defined(i386) && !defined(sequent)
  164.   #define INITIALIZE_HEADER N_SET_MACHTYPE (outheader, M_386)
  165. ! #endif /* Sequent symmetry.  */
  166. ! #if defined(hp300)
  167. ! #define INITIALIZE_HEADER outheader.a_mid = MID_HP300
  168. ! #endif /* hp300.  */
  169. ! #ifdef pyr
  170. ! #define INITIALIZE_HEADER outheader.a_machid = PYR90X
  171. ! #endif /* Pyramid.  */
  172.   
  173.   #ifdef is68k
  174.   /* This enables code to take care of an ugly hack in the ISI OS.
  175. ***************
  176. *** 390,402 ****
  177.   /* Sparc (Sun 4) macros */
  178.   #undef relocation_info
  179.   #define relocation_info                    reloc_info_sparc
  180. ! #define RELOC_ADDRESS(r)        ((r)->r_address)                 
  181. ! #define RELOC_EXTERN_P(r)               ((r)->r_extern)      
  182. ! #define RELOC_TYPE(r)                   ((r)->r_index)  
  183. ! #define RELOC_SYMBOL(r)                 ((r)->r_index)   
  184.   #define RELOC_MEMORY_SUB_P(r)        0
  185.   #define RELOC_MEMORY_ADD_P(r)           0
  186. ! #define RELOC_ADD_EXTRA(r)              ((r)->r_addend)       
  187.   #define RELOC_PCREL_P(r)             \
  188.           ((r)->r_type >= RELOC_DISP8 && (r)->r_type <= RELOC_WDISP22)
  189.   #define RELOC_VALUE_RIGHTSHIFT(r)       (reloc_target_rightshift[(r)->r_type])
  190. --- 445,457 ----
  191.   /* Sparc (Sun 4) macros */
  192.   #undef relocation_info
  193.   #define relocation_info                    reloc_info_sparc
  194. ! #define RELOC_ADDRESS(r)        ((r)->r_address)
  195. ! #define RELOC_EXTERN_P(r)               ((r)->r_extern)
  196. ! #define RELOC_TYPE(r)                   ((r)->r_index)
  197. ! #define RELOC_SYMBOL(r)                 ((r)->r_index)
  198.   #define RELOC_MEMORY_SUB_P(r)        0
  199.   #define RELOC_MEMORY_ADD_P(r)           0
  200. ! #define RELOC_ADD_EXTRA(r)              ((r)->r_addend)
  201.   #define RELOC_PCREL_P(r)             \
  202.           ((r)->r_type >= RELOC_DISP8 && (r)->r_type <= RELOC_WDISP22)
  203.   #define RELOC_VALUE_RIGHTSHIFT(r)       (reloc_target_rightshift[(r)->r_type])
  204. ***************
  205. *** 1124,1171 ****
  206.   
  207.     decode_command (argc, argv);
  208.   
  209. -   /* Create the symbols `etext', `edata' and `end'.  */
  210. -   if (!relocatable_output)
  211. -     symtab_init ();
  212. -   /* Determine whether to count the header as part of
  213. -      the text size, and initialize the text size accordingly.
  214. -      This depends on the kind of system and on the output format selected.  */
  215. -   N_SET_MAGIC (outheader, magic);
  216. - #ifdef INITIALIZE_HEADER
  217. -   INITIALIZE_HEADER;
  218. - #endif
  219. -   text_size = sizeof (struct exec);
  220. - #ifdef COFF_ENCAPSULATE
  221. -   if (relocatable_output == 0)
  222. -     {
  223. -       need_coff_header = 1;
  224. -       /* set this flag now, since it will change the values of N_TXTOFF, etc */
  225. -       N_SET_FLAGS (outheader, N_FLAGS_COFF_ENCAPSULATE);
  226. -       text_size += sizeof (struct coffheader);
  227. -     }
  228. - #endif
  229. -   text_size -= N_TXTOFF (outheader);
  230. -   if (text_size < 0)
  231. -     text_size = 0;
  232. -   entry_offset = text_size;
  233. -   if (!T_flag_specified && !relocatable_output)
  234. -     text_start = N_TXTADDR (outheader);
  235. -   /* The text-start address is normally this far past a page boundary.  */
  236. -   text_start_alignment = text_start % page_size;
  237.     /* Load symbols of all input files.
  238.        Also search all libraries and decide which library members to load.  */
  239.   
  240.     load_symbols ();
  241.   
  242.     /* Compute where each file's sections go, and relocate symbols.  */
  243.   
  244.     digest_symbols ();
  245. --- 1283,1300 ----
  246.   
  247.     decode_command (argc, argv);
  248.   
  249.     /* Load symbols of all input files.
  250.        Also search all libraries and decide which library members to load.  */
  251.   
  252.     load_symbols ();
  253.   
  254. +   /* Create various built-in symbols.  This must occur after
  255. +      all input files are loaded so that a user program can have a
  256. +      symbol named etext (for example).  */
  257. +   if (output_style != OUTPUT_RELOCATABLE)
  258. +     symtab_init ();
  259.     /* Compute where each file's sections go, and relocate symbols.  */
  260.   
  261.     digest_symbols ();
  262. ***************
  263. *** 1187,1238 ****
  264.     exit (!make_executable);
  265.   }
  266.   
  267. ! void decode_option ();
  268. ! /* Analyze a command line argument.
  269. !    Return 0 if the argument is a filename.
  270. !    Return 1 if the argument is a option complete in itself.
  271. !    Return 2 if the argument is a option which uses an argument.
  272. !    Thus, the value is the number of consecutive arguments
  273. !    that are part of options.  */
  274. ! int
  275. ! classify_arg (arg)
  276. !      register char *arg;
  277. ! {
  278. !   if (*arg != '-') return 0;
  279. !   switch (arg[1])
  280. !     {
  281. !     case 'A':
  282. !     case 'D':
  283. !     case 'e':
  284. !     case 'L':
  285. !     case 'l':
  286. !     case 'o':
  287. !     case 'u':
  288. !     case 'V':
  289. !     case 'y':
  290. !       if (arg[2])
  291. !     return 1;
  292. !       return 2;
  293. !     case 'B':
  294. !       if (! strcmp (&arg[2], "static"))
  295. !     return 1;
  296. !     case 'T':
  297. !       if (arg[2] == 0)
  298. !     return 2;
  299. !       if (! strcmp (&arg[2], "text"))
  300. !     return 2;
  301. !       if (! strcmp (&arg[2], "data"))
  302. !     return 2;
  303. !       return 1;
  304. !     }
  305.   
  306. !   return 1;
  307. ! }
  308.   
  309.   /* Process the command arguments,
  310.      setting up file_table with an entry for each input file,
  311. --- 1316,1357 ----
  312.     exit (!make_executable);
  313.   }
  314.   
  315. ! void add_cmdline_ref ();
  316.   
  317. ! static struct option longopts[] =
  318. ! {
  319. !   {"d", 0, 0, 'd'},
  320. !   {"dc", 0, 0, 'd'},        /* For Sun compatibility. */
  321. !   {"dp", 0, 0, 'd'},        /* For Sun compatibility. */
  322. !   {"e", 1, 0, 'e'},
  323. !   {"n", 0, 0, 'n'},
  324. !   {"noinhibit-exec", 0, 0, 130},
  325. !   {"nostdlib", 0, 0, 133},
  326. !   {"o", 1, 0, 'o'},
  327. !   {"r", 0, 0, 'r'},
  328. !   {"s", 0, 0, 's'},
  329. !   {"t", 0, 0, 't'},
  330. !   {"u", 1, 0, 'u'},
  331. !   {"x", 0, 0, 'x'},
  332. !   {"z", 0, 0, 'z'},
  333. !   {"A", 1, 0, 'A'},
  334. !   {"Bstatic", 0, 0, 129},    /* For Sun compatibility. */
  335. !   {"D", 1, 0, 'D'},
  336. !   {"M", 0, 0, 'M'},
  337. !   {"N", 0, 0, 'N'},
  338. !   {"S", 0, 0, 'S'},
  339. !   {"T", 1, 0, 'T'},
  340. !   {"Ttext", 1, 0, 'T'},
  341. !   {"Tdata", 1, 0, 132},
  342. !   {"V", 1, 0, 'V'},
  343. !   {"X", 0, 0, 'X'},
  344. !   {0, 0, 0, 0}
  345. ! };
  346. ! /* Since the Unix ld accepts -lfoo, -Lfoo, and -yfoo, we must also.
  347. !    This effectively prevents any long options from starting with
  348. !    one of these letters. */
  349. ! #define SHORTOPTS "-l:y:L:"
  350.   
  351.   /* Process the command arguments,
  352.      setting up file_table with an entry for each input file,
  353. ***************
  354. *** 1116,1211 ****
  355.     n_search_dirs = 0;
  356.     search_dirs = (char **) xmalloc (sizeof (char *));
  357.   
  358. !   /* First compute number_of_files so we know how long to make file_table.  */
  359. !   /* Also process most options completely.  */
  360.   
  361. !   for (i = 1; i < argc; i++)
  362. !     {
  363. !       register int code = classify_arg (argv[i]);
  364. !       if (code)
  365. !     {
  366. !       if (i + code > argc)
  367. !         fatal ("no argument following %s\n", argv[i]);
  368. !       decode_option (argv[i], argv[i+1]);
  369. !       if (argv[i][1] == 'l' || argv[i][1] == 'A')
  370. !         number_of_files++;
  371. !       i += code - 1;
  372.       }
  373. -       else
  374. -     number_of_files++;
  375.       }
  376.   
  377.     if (!number_of_files)
  378. !     fatal ("no input files", 0);
  379.   
  380.     p = file_table
  381.       = (struct file_entry *) xmalloc (number_of_files * sizeof (struct file_entry));
  382.     bzero (p, number_of_files * sizeof (struct file_entry));
  383.   
  384. !   /* Now scan again and fill in file_table.  */
  385. !   /* All options except -A and -l are ignored here.  */
  386.   
  387. !   for (i = 1; i < argc; i++)
  388. !     {
  389. !       register int code = classify_arg (argv[i]);
  390. !       if (code)
  391. !     {
  392. !       char *string;
  393. !       if (code == 2)
  394. !         string = argv[i+1];
  395. !       else
  396. !         string = &argv[i][2];
  397. !       if (argv[i][1] == 'A')
  398. !         {
  399. !           if (p != file_table)
  400. !         fatal ("-A specified before an input file other than the first");
  401. !           p->filename = string;
  402. !           p->local_sym_name = string;
  403. !           p->just_syms_flag = 1;
  404. !           p++;
  405. !         }
  406. !       if (argv[i][1] == 'l')
  407. !         {
  408. !           p->filename = concat ("lib", string, ".a");
  409. !           p->local_sym_name = concat ("-l", string, "");
  410. !           p->search_dirs_flag = 1;
  411. !           p++;
  412. !         }
  413. !       i += code - 1;
  414. !     }
  415. !       else
  416.       {
  417. !       p->filename = argv[i];
  418. !       p->local_sym_name = argv[i];
  419.         p++;
  420.       }
  421.       }
  422.   
  423.     /* Now check some option settings for consistency.  */
  424.   
  425. ! #ifdef NMAGIC
  426. !   if ((magic == ZMAGIC || magic == NMAGIC)
  427. ! #else
  428. !   if ((magic == ZMAGIC)
  429. ! #endif
  430.         && (text_start - text_start_alignment) & (page_size - 1))
  431. !     fatal ("-T argument not multiple of page size, with sharable output", 0);
  432.   
  433.     /* Append the standard search directories to the user-specified ones.  */
  434. !   {
  435. !     int n = sizeof standard_search_dirs / sizeof standard_search_dirs[0];
  436. !     n_search_dirs += n;
  437. !     search_dirs
  438. !       = (char **) xrealloc (search_dirs, n_search_dirs * sizeof (char *));
  439. !     bcopy (standard_search_dirs, &search_dirs[n_search_dirs - n],
  440. !        n * sizeof (char *));
  441. !   }
  442.   }
  443.   
  444.   
  445. --- 1235,1468 ----
  446.     n_search_dirs = 0;
  447.     search_dirs = (char **) xmalloc (sizeof (char *));
  448.   
  449. !   /* First compute number_of_files so we know how long to make file_table.
  450. !      Also process most options completely.  */
  451.   
  452. !   while ((optc = getopt_long_only (argc, argv, SHORTOPTS, longopts, &longind))
  453. !      != EOF)
  454. !     {
  455. !       if (optc == 0)
  456. !     optc = longopts[longind].val;
  457. !       switch (optc)
  458. !     {
  459. !     case '?':
  460. !       usage (0, 0);
  461. !       break;
  462. !     case 1:
  463. !       /* Non-option argument. */
  464. !       number_of_files++;
  465. !       break;
  466. !     case 'd':
  467. !       force_common_definition = 1;
  468. !       break;
  469. !     case 'e':
  470. !       entry_symbol = getsym (optarg);
  471. !       if (!entry_symbol->defined && !entry_symbol->referenced)
  472. !         undefined_global_sym_count++;
  473. !       entry_symbol->referenced = 1;
  474. !       add_cmdline_ref (entry_symbol);
  475. !       break;
  476. !     case 'l':
  477. !       number_of_files++;
  478. !       break;
  479. !     case 'n':
  480. !       if (output_style && output_style != OUTPUT_READONLY_TEXT)
  481. !         fatal ("illegal combination of -n with -N, -r, or -z", (char *) 0);
  482. !       output_style = OUTPUT_READONLY_TEXT;
  483. !       break;
  484. !     case 130:        /* -noinhibit-exec */
  485. !       force_executable = 1;
  486. !       break;
  487. !     case 133:        /* -nostdlib */
  488. !       no_standard_dirs = 1;
  489. !       break;
  490. !     case 'o':
  491. !       output_filename = optarg;
  492. !       break;
  493. !     case 'r':
  494. !       if (output_style && output_style != OUTPUT_RELOCATABLE)
  495. !         fatal ("illegal combination of -r with -N, -n, or -z", (char *) 0);
  496. !       output_style = OUTPUT_RELOCATABLE;
  497. !       text_start = 0;
  498. !       break;
  499. !     case 's':
  500. !       strip_symbols = STRIP_ALL;
  501. !       break;
  502. !     case 't':
  503. !       trace_files = 1;
  504. !       break;
  505. !     case 'u':
  506. !       {
  507. !         register symbol *sp = getsym (optarg);
  508. !         if (!sp->defined && !sp->referenced)
  509. !           undefined_global_sym_count++;
  510. !         sp->referenced = 1;
  511. !         add_cmdline_ref (sp);
  512. !       }
  513. !       break;
  514. !     case 'x':
  515. !       discard_locals = DISCARD_ALL;
  516. !       break;
  517. !       
  518. !     case 'y':
  519. !       {
  520. !         register symbol *sp = getsym (optarg);
  521. !         sp->trace = 1;
  522. !       }
  523. !       break;
  524. !     case 'z':
  525. !       if (output_style && output_style != OUTPUT_DEMAND_PAGED)
  526. !         fatal ("illegal combination of -z with -N, -n, or -r", (char *) 0);
  527. !       output_style = OUTPUT_DEMAND_PAGED;
  528. !       break;
  529. !     case 'A':
  530. !       number_of_files++;
  531. !       break;
  532. !     case 129:        /* -Bstatic. */
  533. !       /* Ignore. */
  534. !       break;
  535. !     case 'D':
  536. !       specified_data_size = parse (optarg, "%x", "invalid argument to -D");
  537. !       break;
  538. !     case 'L':
  539. !       n_search_dirs++;
  540. !       search_dirs = (char **)
  541. !         xrealloc (search_dirs, n_search_dirs * sizeof (char *));
  542. !       search_dirs[n_search_dirs - 1] = optarg;
  543. !       break;
  544. !       
  545. !     case 'M':
  546. !       write_map = 1;
  547. !       break;
  548. !     case 'N':
  549. !       if (output_style && output_style != OUTPUT_WRITABLE_TEXT)
  550. !         fatal ("illegal combination of -N with -n, -r, or -z", (char *) 0);
  551. !       output_style = OUTPUT_WRITABLE_TEXT;
  552. !       break;
  553. !     case 'S':
  554. !       strip_symbols = STRIP_DEBUGGER;
  555. !       break;
  556. !     case 'T':
  557. !       text_start = parse (optarg, "%x", "invalid argument to -Ttext");
  558. !       T_flag_specified = 1;
  559. !       break;
  560. !     case 132:        /* -Tdata addr */
  561. !       data_start = parse (optarg, "%x", "invalid argument to -Tdata");
  562. !       Tdata_flag_specified = 1;
  563. !       break;
  564. !     case 'V':
  565. !       {
  566. !         struct string_list_element *new
  567. !           = (struct string_list_element *)
  568. !         xmalloc (sizeof (struct string_list_element));
  569. !         
  570. !         new->str = optarg;
  571. !         new->next = set_element_prefixes;
  572. !         set_element_prefixes = new;
  573. !       }
  574. !       break;
  575. !     case 'X':
  576. !       discard_locals = DISCARD_L;
  577. !       break;
  578.       }
  579.       }
  580.   
  581.     if (!number_of_files)
  582. !     usage ("no input files", 0);
  583.   
  584.     p = file_table
  585.       = (struct file_entry *) xmalloc (number_of_files * sizeof (struct file_entry));
  586.     bzero (p, number_of_files * sizeof (struct file_entry));
  587.   
  588. !   /* Now scan again and fill in file_table.
  589. !      All options except -A and -l are ignored here.  */
  590.   
  591. !   optind = 0;            /* Reset getopt. */
  592. !   while ((optc = getopt_long_only (argc, argv, SHORTOPTS, longopts, &longind))
  593. !      != EOF)
  594. !     {
  595. !       if (optc == 0)
  596. !     optc = longopts[longind].val;
  597. !       switch (optc)
  598.       {
  599. !     case 1:
  600. !       /* Non-option argument. */
  601. !       p->filename = optarg;
  602. !       p->local_sym_name = optarg;
  603. !       p++;
  604. !       break;
  605. !     case 'A':
  606. !       if (p != file_table)
  607. !         usage ("-A specified before an input file other than the first");
  608. !       p->filename = optarg;
  609. !       p->local_sym_name = optarg;
  610. !       p->just_syms_flag = 1;
  611. !       p++;
  612. !       break;
  613. !     case 'l':
  614. !       p->filename = concat ("lib", optarg, ".a");
  615. !       p->local_sym_name = concat ("-l", optarg, "");
  616. !       p->search_dirs_flag = 1;
  617.         p++;
  618. +       break;
  619.       }
  620.       }
  621.   
  622. +   if (!output_file_type)
  623. +     output_file_type = DEFAULT_OUTPUT_FILE_TYPE;
  624. +   if (!output_style)
  625. +     output_style = DEFAULT_OUTPUT_STYLE;
  626. + #if 0
  627. +   /* THIS CONSISTENCY CHECK BELONGS SOMEWHERE ELSE.  */
  628.     /* Now check some option settings for consistency.  */
  629.   
  630. !   if ((output_style == OUTPUT_READONLY_TEXT || output_style == OUTPUT_DEMAND_PAGED)
  631.         && (text_start - text_start_alignment) & (page_size - 1))
  632. !     usage ("-T argument not multiple of page size, with sharable output", 0);
  633. ! #endif
  634.   
  635.     /* Append the standard search directories to the user-specified ones.  */
  636. !   if (!no_standard_dirs)
  637. !     {
  638. !       int n = sizeof standard_search_dirs / sizeof standard_search_dirs[0];
  639. !       n_search_dirs += n;
  640. !       search_dirs
  641. !     = (char **) xrealloc (search_dirs, n_search_dirs * sizeof (char *));
  642. !       bcopy (standard_search_dirs, &search_dirs[n_search_dirs - n],
  643. !          n * sizeof (char *));
  644. !     }
  645.   }
  646.   
  647.   
  648. ***************
  649. *** 1252,1415 ****
  650.       }
  651.     return 0;
  652.   }
  653. - int parse ();
  654. - /* Record an option and arrange to act on it later.
  655. -    ARG should be the following command argument,
  656. -    which may or may not be used by this option.
  657. -    The `l' and `A' options are ignored here since they actually
  658. -    specify input files.  */
  659. - void
  660. - decode_option (swt, arg)
  661. -      register char *swt, *arg;
  662. - {
  663. -   /* We get Bstatic from gcc on suns.  */
  664. -   if (! strcmp (swt + 1, "Bstatic"))
  665. -     return;
  666. -   if (! strcmp (swt + 1, "Ttext"))
  667. -     {
  668. -       text_start = parse (arg, "%x", "invalid argument to -Ttext");
  669. -       T_flag_specified = 1;
  670. -       return;
  671. -     }
  672. -   if (! strcmp (swt + 1, "Tdata"))
  673. -     {
  674. -       data_start = parse (arg, "%x", "invalid argument to -Tdata");
  675. -       Tdata_flag_specified = 1;
  676. -       return;
  677. -     }
  678. -   if (! strcmp (swt + 1, "noinhibit-exec"))
  679. -     {
  680. -       force_executable = 1;
  681. -       return;
  682. -     }
  683. -   if (swt[2] != 0)
  684. -     arg = &swt[2];
  685. -   switch (swt[1])
  686. -     {
  687. -     case 'A':
  688. -       return;
  689. -     case 'D':
  690. -       specified_data_size = parse (arg, "%x", "invalid argument to -D");
  691. -       return;
  692. -     case 'd':
  693. -       force_common_definition = 1;
  694. -       return;
  695. -     case 'e':
  696. -       entry_symbol = getsym (arg);
  697. -       if (!entry_symbol->defined && !entry_symbol->referenced)
  698. -     undefined_global_sym_count++;
  699. -       entry_symbol->referenced = 1;
  700. -       add_cmdline_ref (entry_symbol);
  701. -       return;
  702. -     case 'l':
  703. -       return;
  704. -     case 'L':
  705. -       n_search_dirs++;
  706. -       search_dirs
  707. -     = (char **) xrealloc (search_dirs, n_search_dirs * sizeof (char *));
  708. -       search_dirs[n_search_dirs - 1] = arg;
  709. -       return;
  710. -     case 'M':
  711. -       write_map = 1;
  712. -       return;
  713. -     case 'N':
  714. -       magic = OMAGIC;
  715. -       return;
  716. - #ifdef NMAGIC
  717. -     case 'n':
  718. -       magic = NMAGIC;
  719. -       return;
  720. - #endif
  721. -     case 'o':
  722. -       output_filename = arg;
  723. -       return;
  724. -     case 'r':
  725. -       relocatable_output = 1;
  726. -       magic = OMAGIC;
  727. -       text_start = 0;
  728. -       return;
  729. -     case 'S':
  730. -       strip_symbols = STRIP_DEBUGGER;
  731. -       return;
  732. -     case 's':
  733. -       strip_symbols = STRIP_ALL;
  734. -       return;
  735. -     case 'T':
  736. -       text_start = parse (arg, "%x", "invalid argument to -T");
  737. -       T_flag_specified = 1;
  738. -       return;
  739. -     case 't':
  740. -       trace_files = 1;
  741. -       return;
  742. -     case 'u':
  743. -       {
  744. -     register symbol *sp = getsym (arg);
  745. -     if (!sp->defined && !sp->referenced)
  746. -       undefined_global_sym_count++;
  747. -     sp->referenced = 1;
  748. -     add_cmdline_ref (sp);
  749. -       }
  750. -       return;
  751. -     case 'V':
  752. -       {
  753. -     struct string_list_element *new
  754. -       = (struct string_list_element *)
  755. -         xmalloc (sizeof (struct string_list_element));
  756. -     new->str = arg;
  757. -     new->next = set_element_prefixes;
  758. -     set_element_prefixes = new;
  759. -     return;
  760. -       }
  761. -     case 'X':
  762. -       discard_locals = DISCARD_L;
  763. -       return;
  764. -     case 'x':
  765. -       discard_locals = DISCARD_ALL;
  766. -       return;
  767. -     case 'y':
  768. -       {
  769. -     register symbol *sp = getsym (&swt[2]);
  770. -     sp->trace = 1;
  771. -       }
  772. -       return;
  773. -     case 'z':
  774. -       magic = ZMAGIC;
  775. -       return;
  776. -     default:
  777. -       fatal ("invalid command option `%s'", swt);
  778. -     }
  779. - }
  780.   
  781. ! /** Convenient functions for operating on one or all files being */
  782. !  /** loaded.  */
  783.   void print_file_name ();
  784.   
  785.   /* Call FUNCTION on each input file entry.
  786. --- 1509,1517 ----
  787.       }
  788.     return 0;
  789.   }
  790.   
  791. ! /* Convenient functions for operating on one or all files being
  792. !    loaded.  */
  793.   void print_file_name ();
  794.   
  795.   /* Call FUNCTION on each input file entry.
  796. ***************
  797. *** 1525,1533 ****
  798.   
  799.     if (input_file) file_close ();
  800.   
  801. !   if (entry->search_dirs_flag)
  802.       {
  803. -       register char **p = search_dirs;
  804.         int i;
  805.   
  806.         for (i = 0; i < n_search_dirs; i++)
  807. --- 1627,1634 ----
  808.   
  809.     if (input_file) file_close ();
  810.   
  811. !   if (entry->search_dirs_flag && n_search_dirs)
  812.       {
  813.         int i;
  814.   
  815.         for (i = 0; i < n_search_dirs; i++)
  816. ***************
  817. *** 1790,1812 ****
  818.        int desc;
  819.        register struct file_entry *entry;
  820.   {
  821. !   register int len;
  822. !   struct exec *loc = (struct exec *) &entry->header;
  823.   
  824. !   lseek (desc, entry->starting_offset, 0);
  825. !   len = read (desc, loc, sizeof (struct exec));
  826. !   if (len != sizeof (struct exec))
  827. !     fatal_with_file ("failure reading header of ", entry);
  828. !   if (N_BADMAG (*loc))
  829. !     fatal_with_file ("bad magic number in ", entry);
  830.   
  831.     entry->header_read_flag = 1;
  832.   }
  833.   
  834.   /* Read the symbols of file ENTRY into core.
  835. !    Assume it is already open, on descriptor DESC.
  836. !    Also read the length of the string table, which follows the symbol table,
  837. !    but don't read the contents of the string table.  */
  838.   
  839.   void
  840.   read_entry_symbols (desc, entry)
  841. --- 2173,2210 ----
  842.        int desc;
  843.        register struct file_entry *entry;
  844.   {
  845. !   if (!entry->file_type)
  846. !     deduce_file_type (desc, entry);
  847.   
  848. !   switch (entry->file_type)
  849. !     {
  850. !     case IS_ARCHIVE:
  851. !     default:
  852. !       /* Should never happen. */
  853. !       abort ();
  854. ! #ifdef A_OUT
  855. !     case IS_A_OUT:
  856. !       read_a_out_header (desc, entry);
  857. !       break;
  858. ! #endif
  859.   
  860. + #ifdef MACH_O
  861. +     case IS_MACH_O:
  862. +       read_mach_o_header (desc, entry);
  863. +       break;
  864. + #endif
  865. +     }
  866.     entry->header_read_flag = 1;
  867.   }
  868.   
  869. + #ifdef MACH_O
  870. + void translate_mach_o_symbols ();
  871. + #endif
  872.   /* Read the symbols of file ENTRY into core.
  873. !    Assume it is already open, on descriptor DESC.  */
  874.   
  875.   void
  876.   read_entry_symbols (desc, entry)
  877. ***************
  878. *** 1818,1839 ****
  879.     if (!entry->header_read_flag)
  880.       read_header (desc, entry);
  881.   
  882. !   entry->symbols = (struct nlist *) xmalloc (entry->header.a_syms);
  883.   
  884. !   lseek (desc, N_SYMOFF (entry->header) + entry->starting_offset, 0);
  885. !   if (entry->header.a_syms != read (desc, entry->symbols, entry->header.a_syms))
  886.       fatal_with_file ("premature end of file in symbols of ", entry);
  887.   
  888. !   lseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
  889. !   if (sizeof str_size != read (desc, &str_size, sizeof str_size))
  890. !     fatal_with_file ("bad string table size in ", entry);
  891. !   entry->string_size = str_size;
  892.   }
  893.   
  894.   /* Read the string table of file ENTRY into core.
  895. !    Assume it is already open, on descriptor DESC.
  896. !    Also record whether a GDB symbol segment follows the string table.  */
  897.   
  898.   void
  899.   read_entry_strings (desc, entry)
  900. --- 2216,2235 ----
  901.     if (!entry->header_read_flag)
  902.       read_header (desc, entry);
  903.   
  904. !   entry->symbols = (struct nlist *) xmalloc (entry->syms_size);
  905.   
  906. !   lseek (desc, entry->syms_offset + entry->starting_offset, 0);
  907. !   if (entry->syms_size != read (desc, entry->symbols, entry->syms_size))
  908.       fatal_with_file ("premature end of file in symbols of ", entry);
  909.   
  910. ! #ifdef MACH_O
  911. !   if (entry->file_type == IS_MACH_O)
  912. !     translate_mach_o_symbols (entry);
  913. ! #endif
  914.   }
  915.   
  916.   /* Read the string table of file ENTRY into core.
  917. !    Assume it is already open, on descriptor DESC.  */
  918.   
  919.   void
  920.   read_entry_strings (desc, entry)
  921. ***************
  922. *** 1736,1768 ****
  923.        register struct file_entry *entry;
  924.   {
  925.     register int desc;
  926. -   register int len;
  927. -   struct exec hdr;
  928.   
  929.     desc = file_open (entry);
  930.   
  931. !   len = read (desc, &hdr, sizeof hdr);
  932. !   if (len != sizeof hdr)
  933. !     fatal_with_file ("failure reading header of ", entry);
  934.   
  935. !   if (!N_BADMAG (hdr))
  936.       {
  937.         read_entry_symbols (desc, entry);
  938. !       entry->strings = (char *) alloca (entry->string_size);
  939.         read_entry_strings (desc, entry);
  940.         enter_file_symbols (entry);
  941.         entry->strings = 0;
  942.       }
  943. -   else
  944. -     {
  945. -       char armag[SARMAG];
  946. -       lseek (desc, 0, 0);
  947. -       if (SARMAG != read (desc, armag, SARMAG) || strncmp (armag, ARMAG, SARMAG))
  948. -     fatal_with_file ("malformed input file (not rel or archive) ", entry);
  949. -       entry->library_flag = 1;
  950. -       search_library (desc, entry);
  951. -     }
  952.   
  953.     file_close ();
  954.   }
  955. --- 2113,2137 ----
  956.        register struct file_entry *entry;
  957.   {
  958.     register int desc;
  959.   
  960.     desc = file_open (entry);
  961.   
  962. !   if (!entry->file_type)
  963. !     deduce_file_type (desc, entry);
  964.   
  965. !   if (entry->file_type == IS_ARCHIVE)
  966. !     {
  967. !       entry->library_flag = 1;
  968. !       search_library (desc, entry);
  969. !     }
  970. !   else
  971.       {
  972.         read_entry_symbols (desc, entry);
  973. !       entry->strings = (char *) alloca (entry->strs_size);
  974.         read_entry_strings (desc, entry);
  975.         enter_file_symbols (entry);
  976.         entry->strings = 0;
  977.       }
  978.   
  979.     file_close ();
  980.   }
  981. ***************
  982. *** 1775,1782 ****
  983.   {
  984.     register struct nlist
  985.       *p,
  986. !     *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  987. !   int lowest_set_vector = -1;
  988.   
  989.     if (trace_files) prline_file_name (entry, stderr);
  990.   
  991. --- 2144,2150 ----
  992.   {
  993.     register struct nlist
  994.       *p,
  995. !     *end = entry->symbols + entry->syms_size / sizeof (struct nlist);
  996.   
  997.     if (trace_files) prline_file_name (entry, stderr);
  998.   
  999. ***************
  1000. *** 1900,1907 ****
  1001.         /* Indirect symbols value should be modified to point
  1002.            a symbol being equivalenced to. */
  1003.         nlist_p->n_value
  1004.           = (unsigned int) getsym ((nlist_p + 1)->n_un.n_strx
  1005.                        + entry->strings);
  1006.         if ((symbol *) nlist_p->n_value == sp)
  1007.           {
  1008.             /* Somebody redefined a symbol to be itself.  */
  1009. --- 2268,2280 ----
  1010.         /* Indirect symbols value should be modified to point
  1011.            a symbol being equivalenced to. */
  1012.         nlist_p->n_value
  1013. + #ifndef NeXT
  1014.           = (unsigned int) getsym ((nlist_p + 1)->n_un.n_strx
  1015.                        + entry->strings);
  1016. + #else
  1017. +         /* NeXT also has indirection but they do it weirdly. */
  1018. +         = (unsigned int) getsym (nlist_p->n_value + entry->strings);
  1019. + #endif
  1020.         if ((symbol *) nlist_p->n_value == sp)
  1021.           {
  1022.             /* Somebody redefined a symbol to be itself.  */
  1023. ***************
  1024. *** 2371,2377 ****
  1025.        potentially want it.  */
  1026.         if (type & N_EXT
  1027.         && (type != (N_UNDF | N_EXT) || p->n_value
  1028. !           
  1029.   #ifdef DOLLAR_KLUDGE
  1030.              || name[1] == '$'
  1031.   #endif
  1032. --- 2742,2748 ----
  1033.        potentially want it.  */
  1034.         if (type & N_EXT
  1035.         && (type != (N_UNDF | N_EXT) || p->n_value
  1036.   #ifdef DOLLAR_KLUDGE
  1037.              || name[1] == '$'
  1038.   #endif
  1039. ***************
  1040. *** 2493,2526 ****
  1041.     /* If necessary, pad text section to full page in the file.
  1042.        Include the padding in the text segment size.  */
  1043.   
  1044. ! #ifdef NMAGIC
  1045. !   if (magic == ZMAGIC || magic == NMAGIC)
  1046. ! #else
  1047. !   if (magic == ZMAGIC)
  1048. ! #endif
  1049.       {
  1050. !       int text_end = text_size + N_TXTOFF (outheader);
  1051. !       text_pad = ((text_end + page_size - 1) & (- page_size)) - text_end;
  1052.         text_size += text_pad;
  1053.       }
  1054.   
  1055. !   outheader.a_text = text_size;
  1056. ! #ifdef sequent
  1057. !   outheader.a_text += N_ADDRADJ (outheader);
  1058. ! #endif
  1059. !   /* Make the data segment address start in memory on a suitable boundary.  */
  1060.   
  1061. !   if (! Tdata_flag_specified)
  1062. !     data_start = N_DATADDR (outheader) + text_start - N_TXTADDR (outheader);
  1063.   
  1064.     /* Make sure bss starts out aligned as much as anyone can want.  */
  1065.   
  1066. !   data_size = (data_size + sizeof(double) - 1) & ~(sizeof(double)-1);
  1067.   
  1068.     /* Set up the set element vector */
  1069.   
  1070. !   if (!relocatable_output)
  1071.       {
  1072.         /* The set sector size is the number of set elements + a word
  1073.            for each symbol for the length word at the beginning of the
  1074. --- 2872,2899 ----
  1075.     /* If necessary, pad text section to full page in the file.
  1076.        Include the padding in the text segment size.  */
  1077.   
  1078. !   if (output_style == OUTPUT_READONLY_TEXT || output_style == OUTPUT_DEMAND_PAGED)
  1079.       {
  1080. !       text_pad = ((text_size + page_size - 1) & (- page_size)) - text_size;
  1081.         text_size += text_pad;
  1082.       }
  1083.   
  1084. !   /* Now that the text_size is known, initialize the data start address;
  1085. !      this depends on text_size as well as the output file format.  */
  1086.   
  1087. !   initialize_data_start ();
  1088.   
  1089.     /* Make sure bss starts out aligned as much as anyone can want.  */
  1090. +   {
  1091. +     int new_data_size = (data_size + sizeof(double) - 1) & ~(sizeof(double)-1);
  1092.   
  1093. !     data_pad += new_data_size - data_size;
  1094. !     data_size = new_data_size;
  1095. !   }
  1096.   
  1097.     /* Set up the set element vector */
  1098.   
  1099. !   if (output_style != OUTPUT_RELOCATABLE)
  1100.       {
  1101.         /* The set sector size is the number of set elements + a word
  1102.            for each symbol for the length word at the beginning of the
  1103. ***************
  1104. *** 2556,2562 ****
  1105.       {
  1106.         /* For each symbol */
  1107.         register struct nlist *p, *next;
  1108. !       int defs = 0, com = sp->max_common_size, erred = 0;
  1109.         struct nlist *first_definition;
  1110.         for (p = sp->refs; p; p = next)
  1111.           {
  1112. --- 2929,2935 ----
  1113.       {
  1114.         /* For each symbol */
  1115.         register struct nlist *p, *next;
  1116. !       int defs = 0, com = sp->max_common_size;
  1117.         struct nlist *first_definition;
  1118.         for (p = sp->refs; p; p = next)
  1119.           {
  1120. ***************
  1121. *** 2828,2853 ****
  1122.   
  1123.     bss_size = (bss_size + sizeof(double) - 1) & ~(sizeof(double)-1);
  1124.   
  1125. !   if (end_symbol)        /* These are null if -r.  */
  1126. !     {
  1127. !       etext_symbol->value = text_size + text_start;
  1128. !       edata_symbol->value = data_start + data_size;
  1129. !       end_symbol->value = data_start + data_size + bss_size;
  1130. !     }
  1131.   
  1132.     /* Figure the data_pad now, so that it overlaps with the bss addresses.  */
  1133.   
  1134. !   if (specified_data_size && specified_data_size > data_size)
  1135. !     data_pad = specified_data_size - data_size;
  1136.   
  1137. !   if (magic == ZMAGIC)
  1138. !     data_pad = ((data_pad + data_size + page_size - 1) & (- page_size))
  1139. !                - data_size;
  1140. !   bss_size -= data_pad;
  1141. !   if (bss_size < 0) bss_size = 0;
  1142.   
  1143. !   data_size += data_pad;
  1144.   }
  1145.   
  1146.   /* Accumulate the section sizes of input file ENTRY
  1147. --- 3201,3253 ----
  1148.   
  1149.     bss_size = (bss_size + sizeof(double) - 1) & ~(sizeof(double)-1);
  1150.   
  1151. !   /* Give values to _end and friends.  */
  1152. !   {
  1153. !     int end_value = data_start + data_size + bss_size;
  1154. !     if (end_symbol)
  1155. !       end_symbol->value = end_value;
  1156. !     if (end_symbol_alt)
  1157. !       end_symbol_alt->value = end_value;
  1158. !   }
  1159. !   {
  1160. !     int etext_value = text_size + text_start;
  1161. !     if (etext_symbol)
  1162. !       etext_symbol->value = etext_value;
  1163. !     if (etext_symbol_alt)
  1164. !       etext_symbol_alt->value = etext_value;
  1165. !   }
  1166. !   {
  1167. !     int edata_value = data_start + data_size;
  1168. !     if (edata_symbol)
  1169. !       edata_symbol->value = edata_value;
  1170. !     if (edata_symbol_alt)
  1171. !       edata_symbol_alt->value = edata_value;
  1172. !   }
  1173.   
  1174.     /* Figure the data_pad now, so that it overlaps with the bss addresses.  */
  1175.   
  1176. !   {
  1177. !     /* The amount of data_pad that we are computing now.  This is the
  1178. !        part which overlaps with bss.  What was computed previously
  1179. !        goes before bss.  */
  1180. !     int data_pad_additional = 0;
  1181. !     
  1182. !     if (specified_data_size && specified_data_size > data_size)
  1183. !       data_pad_additional = specified_data_size - data_size;
  1184. !     if (output_style == OUTPUT_DEMAND_PAGED)
  1185. !       data_pad_additional =
  1186. !     ((data_pad_additional + data_size + page_size - 1) & (- page_size)) - data_size;
  1187. !     bss_size -= data_pad_additional;
  1188. !     if (bss_size < 0) bss_size = 0;
  1189.   
  1190. !     data_size += data_pad_additional;
  1191.   
  1192. !     data_pad += data_pad_additional;
  1193. !   }
  1194.   }
  1195.   
  1196.   /* Accumulate the section sizes of input file ENTRY
  1197. ***************
  1198. *** 2899,2910 ****
  1199.     current->filename = next->filename;
  1200.   
  1201.     while (++(next->sym) < (entry->symbols
  1202. !               + entry->header.a_syms/sizeof (struct nlist)))
  1203.       {
  1204.         /* n_type is a char, and N_SOL, N_EINCL and N_BINCL are > 0x80, so
  1205.          * may look negative...therefore, must mask to low bits
  1206.          */
  1207. !       switch (next->sym->n_type & 0xff) 
  1208.       {
  1209.       case N_SLINE:
  1210.         if (use_data_symbols) continue;
  1211. --- 3300,3311 ----
  1212.     current->filename = next->filename;
  1213.   
  1214.     while (++(next->sym) < (entry->symbols
  1215. !               + entry->syms_size/sizeof (struct nlist)))
  1216.       {
  1217.         /* n_type is a char, and N_SOL, N_EINCL and N_BINCL are > 0x80, so
  1218.          * may look negative...therefore, must mask to low bits
  1219.          */
  1220. !       switch (next->sym->n_type & 0xff)
  1221.       {
  1222.       case N_SLINE:
  1223.         if (use_data_symbols) continue;
  1224. ***************
  1225. *** 3023,3029 ****
  1226.     int use_data_symbols;
  1227.   
  1228.     if (next->sym)
  1229. !     use_data_symbols = (next->sym->n_type & N_TYPE) == N_DATA;
  1230.     else
  1231.       return current->line;
  1232.   
  1233. --- 3424,3430 ----
  1234.     int use_data_symbols;
  1235.   
  1236.     if (next->sym)
  1237. !     use_data_symbols = (next->sym->n_type & ~N_EXT) == N_DATA;
  1238.     else
  1239.       return current->line;
  1240.   
  1241. ***************
  1242. *** 3075,3081 ****
  1243.       *reloc_start = data_segment ? entry->datarel : entry->textrel,
  1244.       *reloc;
  1245.     int reloc_size
  1246. !     = ((data_segment ? entry->header.a_drsize : entry->header.a_trsize)
  1247.          / sizeof (struct relocation_info));
  1248.     int start_of_segment
  1249.       = (data_segment ? entry->data_start_address : entry->text_start_address);
  1250. --- 3476,3482 ----
  1251.       *reloc_start = data_segment ? entry->datarel : entry->textrel,
  1252.       *reloc;
  1253.     int reloc_size
  1254. !     = ((data_segment ? entry->data_reloc_size : entry->text_reloc_size)
  1255.          / sizeof (struct relocation_info));
  1256.     int start_of_segment
  1257.       = (data_segment ? entry->data_start_address : entry->text_start_address);
  1258. ***************
  1259. *** 3082,3091 ****
  1260.     struct nlist *start_of_syms = entry->symbols;
  1261.     struct line_debug_entry *state_pointer
  1262.       = init_debug_scan (data_segment != 0, entry);
  1263. !   register struct line_debug_entry
  1264. !     *current = state_pointer,
  1265. !     *next = state_pointer + 1,
  1266. !     *source = state_pointer + 2;
  1267.     /* Assigned to generally static values; should not be written into.  */
  1268.     char *errfmt;
  1269.     /* Assigned to alloca'd values cand copied into; should be freed
  1270. --- 3483,3489 ----
  1271.     struct nlist *start_of_syms = entry->symbols;
  1272.     struct line_debug_entry *state_pointer
  1273.       = init_debug_scan (data_segment != 0, entry);
  1274. !   register struct line_debug_entry *current = state_pointer;
  1275.     /* Assigned to generally static values; should not be written into.  */
  1276.     char *errfmt;
  1277.     /* Assigned to alloca'd values cand copied into; should be freed
  1278. ***************
  1279. *** 3307,3324 ****
  1280.   
  1281.         /* Mark as being noted by relocation warning pass.  */
  1282.         SET_BIT (nlist_bitvector, s - start_of_syms);
  1283. !       
  1284.         errfmt = 0;
  1285.         errmsg = g->warning;
  1286.         invalidate_line_number = 0;
  1287.       }
  1288. !       
  1289.   
  1290.         /* If errfmt == 0, errmsg has already been defined.  */
  1291.         if (errfmt != 0)
  1292.       {
  1293. !       errmsg = (char *) xmalloc (strlen (errfmt) + strlen (g->name) + 1);
  1294. !       sprintf (errmsg, errfmt, g->name, data_segment ? "data" : "text");
  1295.       }
  1296.   
  1297.         address_to_line (RELOC_ADDRESS (reloc) + start_of_segment,
  1298. --- 3705,3728 ----
  1299.   
  1300.         /* Mark as being noted by relocation warning pass.  */
  1301.         SET_BIT (nlist_bitvector, s - start_of_syms);
  1302.         errfmt = 0;
  1303.         errmsg = g->warning;
  1304.         invalidate_line_number = 0;
  1305.       }
  1306.   
  1307.         /* If errfmt == 0, errmsg has already been defined.  */
  1308.         if (errfmt != 0)
  1309.       {
  1310. !       char *nm;
  1311. !       if (!demangler || !(nm = (*demangler)(g->name)))
  1312. !         nm = g->name;
  1313. !       errmsg = xmalloc (strlen (errfmt) + strlen (nm) + 1);
  1314. !       sprintf (errmsg, errfmt, nm, data_segment ? "data" : "text");
  1315. !       if (nm != g->name)
  1316. !         free (nm);
  1317.       }
  1318.   
  1319.         address_to_line (RELOC_ADDRESS (reloc) + start_of_segment,
  1320. ***************
  1321. *** 3198,3204 ****
  1322.        struct file_entry *entry;
  1323.        FILE *outfile;
  1324.   {
  1325. !   int number_of_syms = entry->header.a_syms / sizeof (struct nlist);
  1326.     unsigned char *nlist_bitvector
  1327.       = (unsigned char *) alloca ((number_of_syms >> 3) + 1);
  1328.     struct line_debug_entry *text_scan, *data_scan;
  1329. --- 3609,3615 ----
  1330.        struct file_entry *entry;
  1331.        FILE *outfile;
  1332.   {
  1333. !   int number_of_syms = entry->syms_size / sizeof (struct nlist);
  1334.     unsigned char *nlist_bitvector
  1335.       = (unsigned char *) alloca ((number_of_syms >> 3) + 1);
  1336.     struct line_debug_entry *text_scan, *data_scan;
  1337. ***************
  1338. *** 3426,3432 ****
  1339.       {
  1340.         if (g->undef_refs >= MAX_UREFS_PRINTED)
  1341.           continue;
  1342. !       
  1343.         if (++(g->undef_refs) == MAX_UREFS_PRINTED)
  1344.           errfmt = "More undefined \"%s\" refs follow";
  1345.         else
  1346. --- 3837,3843 ----
  1347.       {
  1348.         if (g->undef_refs >= MAX_UREFS_PRINTED)
  1349.           continue;
  1350.         if (++(g->undef_refs) == MAX_UREFS_PRINTED)
  1351.           errfmt = "More undefined \"%s\" refs follow";
  1352.         else
  1353. ***************
  1354. *** 3449,3464 ****
  1355.       }
  1356.         else
  1357.       continue;
  1358. !       
  1359.         if (line_number == -1)
  1360. !     fprintf (outfile, "%s: ", entry->filename);
  1361.         else
  1362. !     fprintf (outfile, "%s:%d: ", file_name, line_number);
  1363.   
  1364.         if (dont_allow_symbol_name)
  1365.       fprintf (outfile, "%s", errfmt);
  1366.         else
  1367. !     fprintf (outfile, errfmt, g->name);
  1368.   
  1369.         fputc ('\n', outfile);
  1370.       }
  1371. --- 3860,3892 ----
  1372.       }
  1373.         else
  1374.       continue;
  1375.         if (line_number == -1)
  1376. !     {
  1377. !       print_file_name (entry, outfile);
  1378. !       fprintf (outfile, ": ");
  1379. !     }
  1380.         else
  1381. !     {
  1382. !       fprintf (outfile, "%s:%d (", file_name, line_number);
  1383. !       print_file_name (entry, outfile);
  1384. !       fprintf (outfile, "): ");
  1385. !     }
  1386.   
  1387.         if (dont_allow_symbol_name)
  1388.       fprintf (outfile, "%s", errfmt);
  1389.         else
  1390. !     {
  1391. !       char *nm;
  1392. !       if (!demangler || !(nm = (*demangler)(g->name)))
  1393. !         fprintf (outfile, errfmt, g->name);
  1394. !       else
  1395. !         {
  1396. !           fprintf (outfile, errfmt, nm);
  1397. !           free (nm);
  1398. !         }
  1399. !     }
  1400.   
  1401.         fputc ('\n', outfile);
  1402.       }
  1403. ***************
  1404. *** 3348,3408 ****
  1405.       make_executable = 0;
  1406.   }
  1407.   
  1408. ! /* Write the output file */
  1409. ! void
  1410. ! write_output ()
  1411. ! {
  1412. !   struct stat statbuf;
  1413. !   int filemode;
  1414. !   outdesc = open (output_filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  1415. !   if (outdesc < 0) perror_name (output_filename);
  1416.   
  1417. !   if (fstat (outdesc, &statbuf) < 0)
  1418. !     perror_name (output_filename);
  1419.   
  1420. !   filemode = statbuf.st_mode;
  1421.   
  1422. !   chmod (output_filename, filemode & ~0111);
  1423.   
  1424. !   /* Output the a.out header.  */
  1425. !   write_header ();
  1426.   
  1427. !   /* Output the text and data segments, relocating as we go.  */
  1428. !   write_text ();
  1429. !   write_data ();
  1430.   
  1431. !   /* Output the merged relocation info, if requested with `-r'.  */
  1432. !   if (relocatable_output)
  1433. !     write_rel ();
  1434.   
  1435. !   /* Output the symbol table (both globals and locals).  */
  1436. !   write_syms ();
  1437.   
  1438. !   /* Copy any GDB symbol segments from input files.  */
  1439. !   write_symsegs ();
  1440.   
  1441. !   close (outdesc);
  1442.   
  1443. !   if (chmod (output_filename, filemode | 0111) == -1)
  1444. !     perror_name (output_filename);
  1445.   }
  1446. ! void modify_location (), perform_relocation (), copy_text (), copy_data ();
  1447.   
  1448.   void
  1449. ! write_header ()
  1450.   {
  1451. -   N_SET_MAGIC (outheader, magic);
  1452.     outheader.a_text = text_size;
  1453.   #ifdef sequent
  1454.     outheader.a_text += N_ADDRADJ (outheader);
  1455.   #endif
  1456.     outheader.a_data = data_size;
  1457.     outheader.a_bss = bss_size;
  1458.     outheader.a_entry = (entry_symbol ? entry_symbol->value
  1459. !                : text_start + entry_offset);
  1460.   #ifdef COFF_ENCAPSULATE
  1461.     if (need_coff_header)
  1462.       {
  1463. --- 3774,3877 ----
  1464.       make_executable = 0;
  1465.   }
  1466.   
  1467. ! #ifdef A_OUT
  1468.   
  1469. ! /* Stuff pertaining to creating a.out files. */
  1470.   
  1471. ! /* The a.out header. */
  1472.   
  1473. ! struct exec outheader;
  1474.   
  1475. ! #ifdef COFF_ENCAPSULATE
  1476. ! int need_coff_header;
  1477. ! struct coffheader coffheader;
  1478. ! #endif
  1479.   
  1480. ! /* Compute text_start and text_header_size for an a.out file.  */
  1481.   
  1482. ! void
  1483. ! initialize_a_out_text_start ()
  1484. ! {
  1485. !   int magic;
  1486.   
  1487. !   switch (output_style)
  1488. !     {
  1489. !     case OUTPUT_RELOCATABLE:
  1490. !     case OUTPUT_WRITABLE_TEXT:
  1491. !       magic = OMAGIC;
  1492. !       break;
  1493. !     case OUTPUT_READONLY_TEXT:
  1494. ! #ifdef NMAGIC
  1495. !       magic = NMAGIC;
  1496. !       break;
  1497. ! #endif
  1498. !     case OUTPUT_DEMAND_PAGED:
  1499. !       magic = ZMAGIC;
  1500. !       break;
  1501. !     default:
  1502. !       fatal ("unknown output style found (bug in ld)", (char *) 0);
  1503. !       break;
  1504. !     }
  1505.   
  1506. !   /* Determine whether to count the header as part of
  1507. !      the text size, and initialize the text size accordingly.
  1508. !      This depends on the kind of system and on the output format selected.  */
  1509. !   N_SET_MAGIC (outheader, magic);
  1510. ! #ifdef INITIALIZE_HEADER
  1511. !   INITIALIZE_HEADER;
  1512. ! #endif
  1513.   
  1514. !   text_header_size = sizeof (struct exec);
  1515. ! #ifdef COFF_ENCAPSULATE
  1516. !   /* Don't write the coff header for the output of ld -A (since
  1517. !      it is not executable by the kernel anyway).  */
  1518. !   if (output_style != OUTPUT_RELOCATABLE && !file_table[0].just_syms_flag)
  1519. !     {
  1520. !       need_coff_header = 1;
  1521. !       /* set this flag now, since it will change the values of N_TXTOFF, etc */
  1522. !       N_SET_FLAGS (outheader, N_FLAGS_COFF_ENCAPSULATE);
  1523. !       text_header_size += sizeof (struct coffheader);
  1524. !     }
  1525. ! #endif
  1526. !   if (text_header_size <= N_TXTOFF (outheader))
  1527. !     text_header_size = 0;
  1528. !   else
  1529. !     text_header_size -= N_TXTOFF (outheader);
  1530. ! #ifdef _N_BASEADDR
  1531. !   /* SunOS 4.1 N_TXTADDR depends on the value of outheader.a_entry. */
  1532. !   outheader.a_entry = N_PAGSIZ(outheader);
  1533. ! #endif
  1534.   
  1535. !   if (!T_flag_specified && output_style != OUTPUT_RELOCATABLE)
  1536. !     text_start = N_TXTADDR (outheader);
  1537.   }
  1538. ! /* Compute data_start once text_size is known. */
  1539.   
  1540.   void
  1541. ! initialize_a_out_data_start ()
  1542.   {
  1543.     outheader.a_text = text_size;
  1544.   #ifdef sequent
  1545.     outheader.a_text += N_ADDRADJ (outheader);
  1546. +   if (entry_symbol == 0)
  1547. +     entry_symbol = getsym ("start");
  1548.   #endif
  1549. +   if (! Tdata_flag_specified)
  1550. +     data_start = N_DATADDR (outheader) + text_start - N_TXTADDR (outheader);
  1551. + }
  1552. + /* Compute offsets of various pieces of the a.out output file.  */
  1553. + void
  1554. + compute_a_out_section_offsets ()
  1555. + {
  1556.     outheader.a_data = data_size;
  1557.     outheader.a_bss = bss_size;
  1558.     outheader.a_entry = (entry_symbol ? entry_symbol->value
  1559. !                : text_start + text_header_size);
  1560.   #ifdef COFF_ENCAPSULATE
  1561.     if (need_coff_header)
  1562.       {
  1563. ***************
  1564. *** 3665,3674 ****
  1565.         outheader.a_drsize = 0;
  1566.       }
  1567.   
  1568.   #ifdef COFF_ENCAPSULATE
  1569.     if (need_coff_header)
  1570.       mywrite (&coffheader, sizeof coffheader, 1, outdesc);
  1571.   #endif
  1572.     mywrite (&outheader, sizeof (struct exec), 1, outdesc);
  1573.   
  1574.     /* Output whatever padding is required in the executable file
  1575. --- 4134,4181 ----
  1576.         outheader.a_drsize = 0;
  1577.       }
  1578.   
  1579. +   /* Initialize the various file offsets.  */
  1580. +   output_text_offset = N_TXTOFF (outheader);
  1581. + #ifdef N_DATOFF
  1582. +   output_data_offset = N_DATOFF (outheader);
  1583. + #else
  1584. +   output_data_offset = output_text_offset + text_size;
  1585. + #endif
  1586. + #ifdef N_TRELOFF
  1587. +   output_trel_offset = N_TRELOFF (outheader);
  1588. + #else
  1589. +   output_trel_offset = output_data_offset + data_size;
  1590. + #endif
  1591. + #ifdef N_DRELOFF
  1592. +   output_drel_offset = N_DRELOFF (outheader);
  1593. + #else
  1594. +   output_drel_offset = output_trel_offset + text_reloc_size;
  1595. + #endif
  1596. +   output_syms_offset = N_SYMOFF (outheader);
  1597. +   output_strs_offset = N_STROFF (outheader);
  1598. + }
  1599. + /* Compute more section offsets once the size of the string table is known.  */
  1600. + void
  1601. + compute_more_a_out_section_offsets ()
  1602. + {
  1603. +   output_symseg_offset = output_strs_offset + output_strs_size;
  1604. + }
  1605. + /* Write the a.out header once everything else is known.  */
  1606. + void
  1607. + write_a_out_header ()
  1608. + {
  1609. +   lseek (outdesc, 0L, 0);
  1610.   #ifdef COFF_ENCAPSULATE
  1611.     if (need_coff_header)
  1612.       mywrite (&coffheader, sizeof coffheader, 1, outdesc);
  1613.   #endif
  1614.     mywrite (&outheader, sizeof (struct exec), 1, outdesc);
  1615.   
  1616.     /* Output whatever padding is required in the executable file
  1617. ***************
  1618. *** 3740,3755 ****
  1619.   
  1620.     if (!entry->datarel)
  1621.       {
  1622. !       reloc = (struct relocation_info *) xmalloc (entry->header.a_drsize);
  1623.         if (desc == -1) desc = file_open (entry);
  1624. !       lseek (desc,
  1625. !          text_offset (entry) + entry->header.a_text
  1626. !          + entry->header.a_data + entry->header.a_trsize,
  1627. !          L_SET);
  1628. !       if (entry->header.a_drsize != read (desc, reloc, entry->header.a_drsize))
  1629.       fatal_with_file ("premature eof in data relocation of ", entry);
  1630.         entry->datarel = reloc;
  1631.       }
  1632.   }
  1633.   
  1634.   /* Read the text segment contents of ENTRY, relocate them,
  1635. --- 4865,4887 ----
  1636.   
  1637.     if (!entry->datarel)
  1638.       {
  1639. !       reloc = (struct relocation_info *) xmalloc (entry->data_reloc_size);
  1640.         if (desc == -1) desc = file_open (entry);
  1641. !       lseek (desc, entry->starting_offset + entry->data_reloc_offset, L_SET);
  1642. !       if (entry->data_reloc_size != read (desc, reloc, entry->data_reloc_size))
  1643.       fatal_with_file ("premature eof in data relocation of ", entry);
  1644.         entry->datarel = reloc;
  1645.       }
  1646. + #ifdef MACH_O
  1647. +   if (entry->file_type == IS_MACH_O)
  1648. +     {
  1649. +       translate_mach_o_relocation (entry, entry->textrel,
  1650. +                    entry->text_reloc_size / sizeof (struct relocation_info));
  1651. +       translate_mach_o_relocation (entry, entry->datarel,
  1652. +                    entry->data_reloc_size / sizeof (struct relocation_info));
  1653. +     }
  1654. + #endif
  1655.   }
  1656.   
  1657.   /* Read the text segment contents of ENTRY, relocate them,
  1658. ***************
  1659. *** 3638,3664 ****
  1660.       }
  1661.     else
  1662.       {
  1663. !       reloc = (struct relocation_info *) alloca (entry->header.a_trsize);
  1664. !       lseek (desc, text_offset (entry) + entry->header.a_text + entry->header.a_data, 0);
  1665. !       if (entry->header.a_trsize != read (desc, reloc, entry->header.a_trsize))
  1666.       fatal_with_file ("premature eof in text relocation of ", entry);
  1667.       }
  1668.   
  1669.     /* Read the text section into core.  */
  1670.   
  1671. !   lseek (desc, text_offset (entry), 0);
  1672. !   if (entry->header.a_text != read (desc, bytes, entry->header.a_text))
  1673.       fatal_with_file ("premature eof in text section of ", entry);
  1674.   
  1675.     /* Relocate the text according to the text relocation.  */
  1676.   
  1677. !   perform_relocation (bytes, entry->text_start_address, entry->header.a_text,
  1678. !               reloc, entry->header.a_trsize, entry);
  1679.   
  1680.     /* Write the relocated text to the output file.  */
  1681.   
  1682. !   mywrite (bytes, 1, entry->header.a_text, outdesc);
  1683.   }
  1684.   
  1685.   /* Relocate the data segment of each input file
  1686. --- 4770,4800 ----
  1687.       }
  1688.     else
  1689.       {
  1690. !       reloc = (struct relocation_info *) alloca (entry->text_reloc_size);
  1691. !       lseek (desc, entry->starting_offset + entry->text_reloc_offset, L_SET);
  1692. !       if (entry->text_reloc_size != read (desc, reloc, entry->text_reloc_size))
  1693.       fatal_with_file ("premature eof in text relocation of ", entry);
  1694. + #ifdef MACH_O
  1695. +       if (entry->file_type == IS_MACH_O)
  1696. +     translate_mach_o_relocation (entry, reloc,
  1697. +                      entry->text_reloc_size / sizeof (struct relocation_info));
  1698. + #endif
  1699.       }
  1700.   
  1701.     /* Read the text section into core.  */
  1702.   
  1703. !   lseek (desc, entry->starting_offset + entry->text_offset, L_SET);
  1704. !   if (entry->text_size != read (desc, bytes, entry->text_size))
  1705.       fatal_with_file ("premature eof in text section of ", entry);
  1706.   
  1707.     /* Relocate the text according to the text relocation.  */
  1708.   
  1709. !   perform_relocation (bytes, entry->text_start_address - entry->orig_text_address,
  1710. !               entry->text_size, reloc, entry->text_reloc_size, entry);
  1711.   
  1712.     /* Write the relocated text to the output file.  */
  1713.   
  1714. !   mywrite (bytes, 1, entry->text_size, outdesc);
  1715.   }
  1716.   
  1717.   /* Relocate the data segment of each input file
  1718. ***************
  1719. *** 3723,3744 ****
  1720.       }
  1721.     else
  1722.       {
  1723. !       reloc = (struct relocation_info *) alloca (entry->header.a_drsize);
  1724. !       lseek (desc, text_offset (entry) + entry->header.a_text
  1725. !          + entry->header.a_data + entry->header.a_trsize,
  1726. !          0);
  1727. !       if (entry->header.a_drsize != read (desc, reloc, entry->header.a_drsize))
  1728.       fatal_with_file ("premature eof in data relocation of ", entry);
  1729.       }
  1730.   
  1731. !   lseek (desc, text_offset (entry) + entry->header.a_text, 0);
  1732. !   if (entry->header.a_data != read (desc, bytes, entry->header.a_data))
  1733.       fatal_with_file ("premature eof in data section of ", entry);
  1734.   
  1735. !   perform_relocation (bytes, entry->data_start_address - entry->header.a_text,
  1736. !               entry->header.a_data, reloc, entry->header.a_drsize, entry);
  1737.   
  1738. !   mywrite (bytes, 1, entry->header.a_data, outdesc);
  1739.   }
  1740.   
  1741.   /* Relocate ENTRY's text or data section contents.
  1742. --- 4861,4885 ----
  1743.       }
  1744.     else
  1745.       {
  1746. !       reloc = (struct relocation_info *) alloca (entry->data_reloc_size);
  1747. !       lseek (desc, entry->starting_offset + entry->data_reloc_offset, L_SET);
  1748. !       if (entry->data_reloc_size != read (desc, reloc, entry->data_reloc_size))
  1749.       fatal_with_file ("premature eof in data relocation of ", entry);
  1750. + #ifdef MACH_O
  1751. +       if (entry->file_type == IS_MACH_O)
  1752. +     translate_mach_o_relocation (entry, reloc,
  1753. +                      entry->data_reloc_size / sizeof (struct relocation_info));
  1754. + #endif
  1755.       }
  1756.   
  1757. !   lseek (desc, entry->starting_offset + entry->data_offset, L_SET);
  1758. !   if (entry->data_size != read (desc, bytes, entry->data_size))
  1759.       fatal_with_file ("premature eof in data section of ", entry);
  1760.   
  1761. !   perform_relocation (bytes, entry->data_start_address - entry->orig_data_address,
  1762. !               entry->data_size, reloc, entry->data_reloc_size, entry);
  1763.   
  1764. !   mywrite (bytes, 1, entry->data_size, outdesc);
  1765.   }
  1766.   
  1767.   /* Relocate ENTRY's text or data section contents.
  1768. ***************
  1769. *** 4044,4049 ****
  1770.         break;
  1771.   
  1772.       case 2:
  1773.         if (RELOC_MEMORY_SUB_P(p))
  1774.           relocation -= mask & *(long *) (data + addr);
  1775.         else if (RELOC_MEMORY_ADD_P(p))
  1776. --- 5179,5200 ----
  1777.         break;
  1778.   
  1779.       case 2:
  1780. + #ifdef CROSS_LINKER
  1781. +       /* This is necessary if the host has stricter alignment
  1782. +          than the target.  Too slow to use all the time.
  1783. +          Also doesn't deal with differing byte-order.  */
  1784. +       {
  1785. +         /* Thing to relocate.  */
  1786. +         long thing;
  1787. +         bcopy (data + addr, &thing, sizeof (thing));
  1788. +         if (RELOC_MEMORY_SUB_P (p))
  1789. +           relocation -= mask & thing;
  1790. +         else if (RELOC_MEMORY_ADD_P (p))
  1791. +           relocation += mask & thing;
  1792. +         thing = (thing & ~mask) | relocation;
  1793. +         bcopy (&thing, data + addr, sizeof (thing));
  1794. +       }
  1795. + #else /* not CROSS_LINKER */
  1796.         if (RELOC_MEMORY_SUB_P(p))
  1797.           relocation -= mask & *(long *) (data + addr);
  1798.         else if (RELOC_MEMORY_ADD_P(p))
  1799. ***************
  1800. *** 4050,4055 ****
  1801.           relocation += mask & *(long *) (data + addr);
  1802.         *(long *) (data + addr) &= ~mask;
  1803.         *(long *) (data + addr) |= relocation;
  1804.         break;
  1805.   
  1806.       default:
  1807. --- 5201,5207 ----
  1808.           relocation += mask & *(long *) (data + addr);
  1809.         *(long *) (data + addr) &= ~mask;
  1810.         *(long *) (data + addr) |= relocation;
  1811. + #endif /* not CROSS_LINKER */
  1812.         break;
  1813.   
  1814.       default:
  1815. ***************
  1816. *** 4050,4063 ****
  1817.         else
  1818.           /* Debugger symbols come first, so have to start this
  1819.              after them.  */
  1820.             RELOC_SYMBOL(p) = (symptr->def_count + nsyms
  1821.                    - defined_global_sym_count
  1822.                    - undefined_global_sym_count
  1823.                    - global_indirect_count);
  1824.       }
  1825.         p++;
  1826.       }
  1827. !   mywrite (entry->textrel, 1, entry->header.a_trsize, outdesc);
  1828.   }
  1829.   
  1830.   void
  1831. --- 5211,5237 ----
  1832.         else
  1833.           /* Debugger symbols come first, so have to start this
  1834.              after them.  */
  1835. + #ifndef NeXT
  1836.             RELOC_SYMBOL(p) = (symptr->def_count + nsyms
  1837.                    - defined_global_sym_count
  1838.                    - undefined_global_sym_count
  1839.                    - global_indirect_count);
  1840. + #else
  1841. +           RELOC_SYMBOL(p) = (symptr->def_count + nsyms
  1842. +                  - defined_global_sym_count
  1843. +                  - undefined_global_sym_count);
  1844. + #endif
  1845.       }
  1846.         p++;
  1847.       }
  1848. ! #ifdef MACH_O
  1849. !   if (output_file_type == IS_MACH_O)
  1850. !     generate_mach_o_relocations(entry->textrel,
  1851. !                 entry->text_reloc_size / sizeof (struct relocation_info));
  1852. ! #endif
  1853. !   mywrite (entry->textrel, 1, entry->text_reloc_size, outdesc);
  1854.   }
  1855.   
  1856.   void
  1857. ***************
  1858. *** 4113,4139 ****
  1859.            + nsyms - defined_global_sym_count
  1860.            - undefined_global_sym_count
  1861.            - global_indirect_count);
  1862.       }
  1863.         p++;
  1864.       }
  1865. !   mywrite (entry->datarel, 1, entry->header.a_drsize, outdesc);
  1866.   }
  1867.   
  1868.   void write_file_syms ();
  1869.   void write_string_table ();
  1870.   
  1871. - /* Offsets and current lengths of symbol and string tables in output file. */
  1872. - int symbol_table_offset;
  1873. - int symbol_table_len;
  1874. - /* Address in output file where string table starts.  */
  1875. - int string_table_offset;
  1876. - /* Offset within string table
  1877. -    where the strings in `strtab_vector' should be written.  */
  1878. - int string_table_len;
  1879.   /* Total size of string table strings allocated so far,
  1880.      including strings in `strtab_vector'.  */
  1881.   int strtab_size;
  1882. --- 5291,5321 ----
  1883.            + nsyms - defined_global_sym_count
  1884.            - undefined_global_sym_count
  1885.            - global_indirect_count);
  1886. + #else
  1887. +         RELOC_SYMBOL(p)
  1888. +           = (((symbol *)
  1889. +           (((struct nlist *)
  1890. +             (((char *)entry->symbols) + symindex))
  1891. +            ->n_un.n_name))
  1892. +          ->def_count
  1893. +          + nsyms - defined_global_sym_count
  1894. +          - undefined_global_sym_count);
  1895. + #endif
  1896.       }
  1897.         p++;
  1898.       }
  1899. ! #ifdef MACH_O
  1900. !   if (output_file_type == IS_MACH_O)
  1901. !     generate_mach_o_relocations(entry->datarel,
  1902. !                 entry->data_reloc_size / sizeof (struct relocation_info));
  1903. ! #endif
  1904. !   mywrite (entry->datarel, 1, entry->data_reloc_size, outdesc);
  1905.   }
  1906.   
  1907.   void write_file_syms ();
  1908.   void write_string_table ();
  1909.   
  1910.   /* Total size of string table strings allocated so far,
  1911.      including strings in `strtab_vector'.  */
  1912.   int strtab_size;
  1913. ***************
  1914. *** 4306,4342 ****
  1915.           *bufp++ = nl;
  1916.           syms_written++;
  1917.           if (nl.n_type == (N_INDR | N_EXT))
  1918.             {
  1919.           struct nlist xtra_ref;
  1920.           xtra_ref.n_type == N_EXT | N_UNDF;
  1921.           xtra_ref.n_un.n_strx
  1922.             = assign_string_table_index (((symbol *) sp->value)->name);
  1923.           xtra_ref.n_other = 0;
  1924.           xtra_ref.n_desc = 0;
  1925.           xtra_ref.n_value = 0;
  1926.           *bufp++ = xtra_ref;
  1927.           syms_written++;
  1928.             }
  1929.         }
  1930.         }
  1931.   
  1932.     /* Output the buffer full of `struct nlist's.  */
  1933.   
  1934. !   lseek (outdesc, symbol_table_offset + symbol_table_len, 0);
  1935.     mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
  1936. !   symbol_table_len += sizeof (struct nlist) * (bufp - buf);
  1937.   
  1938.     if (syms_written != nsyms)
  1939.       fatal ("internal error: wrong number of symbols written into output file", 0);
  1940.   
  1941. !   if (symbol_table_offset + symbol_table_len != string_table_offset)
  1942. !     fatal ("internal error: inconsistent symbol table length", 0);
  1943.   
  1944. !   /* Now the total string table size is known, so write it.
  1945. !      We are already positioned at the right place in the file.  */
  1946.   
  1947. -   mywrite (&strtab_size, sizeof (int), 1, outdesc);  /* we're at right place */
  1948.     /* Write the strings for the global symbols.  */
  1949.   
  1950.     write_string_table ();
  1951. --- 5496,5543 ----
  1952.           *bufp++ = nl;
  1953.           syms_written++;
  1954.           if (nl.n_type == (N_INDR | N_EXT))
  1955. + #ifndef NeXT
  1956.             {
  1957.           struct nlist xtra_ref;
  1958.           xtra_ref.n_type == N_EXT | N_UNDF;
  1959.           xtra_ref.n_un.n_strx
  1960.             = assign_string_table_index (((symbol *) sp->value)->name);
  1961. + #ifdef N_SECT
  1962. +         xtra_ref.n_sect = 0;
  1963. + #else
  1964.           xtra_ref.n_other = 0;
  1965. + #endif
  1966.           xtra_ref.n_desc = 0;
  1967.           xtra_ref.n_value = 0;
  1968.           *bufp++ = xtra_ref;
  1969.           syms_written++;
  1970.             }
  1971. + #else
  1972. +         nl.n_value = assign_string_table_index (((symbol *) sp->value)->name);
  1973. + #endif
  1974.         }
  1975.         }
  1976.   
  1977. + #ifdef MACH_O
  1978. +   if (output_file_type == IS_MACH_O)
  1979. +     generate_mach_o_symbols(buf, bufp - buf);
  1980. + #endif
  1981.     /* Output the buffer full of `struct nlist's.  */
  1982.   
  1983. !   lseek (outdesc, output_syms_offset + output_syms_size, 0);
  1984.     mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
  1985. !   output_syms_size += sizeof (struct nlist) * (bufp - buf);
  1986.   
  1987.     if (syms_written != nsyms)
  1988.       fatal ("internal error: wrong number of symbols written into output file", 0);
  1989.   
  1990. !   /* Now the total string table size is known, so write it into the
  1991. !      first word of the string table.  */
  1992.   
  1993. !   lseek (outdesc, output_strs_offset, 0);
  1994. !   mywrite (&strtab_size, sizeof (int), 1, outdesc);
  1995.   
  1996.     /* Write the strings for the global symbols.  */
  1997.   
  1998.     write_string_table ();
  1999. ***************
  2000. *** 4436,4446 ****
  2001.       }
  2002.       }
  2003.   
  2004.     /* All the symbols are now in BUF; write them.  */
  2005.   
  2006. !   lseek (outdesc, symbol_table_offset + symbol_table_len, 0);
  2007.     mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
  2008. !   symbol_table_len += sizeof (struct nlist) * (bufp - buf);
  2009.   
  2010.     /* Write the string-table data for the symbols just written,
  2011.        using the data in vectors `strtab_vector' and `strtab_lens'.  */
  2012. --- 5641,5656 ----
  2013.       }
  2014.       }
  2015.   
  2016. + #ifdef MACH_O
  2017. +   if (output_file_type == IS_MACH_O)
  2018. +     generate_mach_o_symbols(buf, bufp - buf);
  2019. + #endif
  2020.     /* All the symbols are now in BUF; write them.  */
  2021.   
  2022. !   lseek (outdesc, output_syms_offset + output_syms_size, 0);
  2023.     mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
  2024. !   output_syms_size += sizeof (struct nlist) * (bufp - buf);
  2025.   
  2026.     /* Write the string-table data for the symbols just written,
  2027.        using the data in vectors `strtab_vector' and `strtab_lens'.  */
  2028. ***************
  2029. *** 4639,4715 ****
  2030.   
  2031.     /* Copy the rest of the symbol segment unchanged.  */
  2032.   
  2033. !   if (entry->superfile)
  2034. !     {
  2035. !       /* Library member: number of bytes to copy is determined
  2036. !      from the member's total size.  */
  2037. !       int total = entry->total_size - entry->symseg_offset - sizeof root;
  2038.   
  2039. !       while (total > 0)
  2040. !     {
  2041. !       len = read (indesc, buffer, min (sizeof buffer, total));
  2042. !       if (len != min (sizeof buffer, total))
  2043. !         fatal_with_file ("premature end of file in symbol segment of ", entry);
  2044. !       total -= len;
  2045. !       mywrite (buffer, len, 1, outdesc);
  2046. !     }
  2047. !     }
  2048. !   else
  2049. !     {
  2050. !       /* A separate file: copy until end of file.  */
  2051. !       while (len = read (indesc, buffer, sizeof buffer))
  2052. !     {
  2053. !       mywrite (buffer, len, 1, outdesc);
  2054. !       if (len < sizeof buffer)
  2055. !         break;
  2056. !     }
  2057.       }
  2058.   
  2059.     file_close ();
  2060.   }
  2061.   
  2062.   /* Create the symbol table entries for `etext', `edata' and `end'.  */
  2063.   
  2064.   void
  2065.   symtab_init ()
  2066.   {
  2067. ! #ifndef nounderscore
  2068. !   edata_symbol = getsym ("_edata");
  2069. !   etext_symbol = getsym ("_etext");
  2070. !   end_symbol = getsym ("_end");
  2071. ! #else
  2072. !   edata_symbol = getsym ("edata");
  2073. !   etext_symbol = getsym ("etext");
  2074. !   end_symbol = getsym ("end");
  2075. ! #endif
  2076.   
  2077.   #ifdef sun
  2078.     {
  2079. !     symbol *dynamic_symbol = getsym ("__DYNAMIC");
  2080. !     dynamic_symbol->defined = N_ABS | N_EXT;
  2081. !     dynamic_symbol->referenced = 1;
  2082. !     dynamic_symbol->value = 0;
  2083.     }
  2084. ! #endif
  2085. ! #ifdef sequent
  2086. !   {
  2087. !     symbol *_387_flt_symbol = getsym ("_387_flt");
  2088. !     _387_flt_symbol->defined = N_ABS | N_EXT;
  2089. !     _387_flt_symbol->referenced = 1;
  2090. !     _387_flt_symbol->value = 0;
  2091. !   }
  2092. ! #endif
  2093. !   edata_symbol->defined = N_DATA | N_EXT;
  2094. !   etext_symbol->defined = N_TEXT | N_EXT;
  2095. !   end_symbol->defined = N_BSS | N_EXT;
  2096. !   edata_symbol->referenced = 1;
  2097. !   etext_symbol->referenced = 1;
  2098. !   end_symbol->referenced = 1;
  2099.   }
  2100.   
  2101.   /* Compute the hash code for symbol name KEY.  */
  2102. --- 5850,5947 ----
  2103.   
  2104.     /* Copy the rest of the symbol segment unchanged.  */
  2105.   
  2106. !   total = entry->symseg_size - sizeof root;
  2107.   
  2108. !   while (total > 0)
  2109. !     {
  2110. !       len = read (indesc, buffer, min (sizeof buffer, total));
  2111. !       if (len != min (sizeof buffer, total))
  2112. !     fatal_with_file ("premature end of file in symbol segment of ", entry);
  2113. !       total -= len;
  2114. !       mywrite (buffer, len, 1, outdesc);
  2115.       }
  2116.   
  2117.     file_close ();
  2118.   }
  2119.   
  2120. + /* Define a special symbol (etext, edata, or end).  NAME is the
  2121. +    name of the symbol, with a leading underscore (whether or not this
  2122. +    system uses such underscores).  TYPE is its type (e.g. N_DATA | N_EXT).
  2123. +    Store a symbol * for the symbol in *SYM if SYM is non-NULL.  */
  2124. + static void
  2125. + symbol_define (name, type, sym)
  2126. +      /* const */ char *name;
  2127. +      int type;
  2128. +      symbol **sym;
  2129. + {
  2130. +   symbol *thesym;
  2131. + #if defined(nounderscore)
  2132. +   /* Skip the leading underscore.  */
  2133. +   name++;
  2134. + #endif
  2135. +   thesym = getsym (name);
  2136. +   if (thesym->defined)
  2137. +     {
  2138. +       /* The symbol is defined in some input file.  Don't mess with it.  */
  2139. +       if (sym)
  2140. +     *sym = 0;
  2141. +     }
  2142. +   else
  2143. +     {
  2144. +       if (thesym->referenced)
  2145. +     /* The symbol was not defined, and we are defining it now.  */
  2146. +     undefined_global_sym_count--;
  2147. +       thesym->defined = type;
  2148. +       thesym->referenced = 1;
  2149. +       if (sym)
  2150. +     *sym = thesym;
  2151. +     }
  2152. + }
  2153.   /* Create the symbol table entries for `etext', `edata' and `end'.  */
  2154.   
  2155.   void
  2156.   symtab_init ()
  2157.   {
  2158. !   symbol_define ("_edata", N_DATA | N_EXT, &edata_symbol);
  2159. !   symbol_define ("_etext", N_TEXT | N_EXT, &etext_symbol);
  2160. !   symbol_define ("_end", N_BSS | N_EXT, &end_symbol);
  2161. !   /* Either _edata or __edata (C names) is OK as far as ANSI is concerned
  2162. !      (see section 4.1.2.1).  In general, it is best to use __foo and
  2163. !      not worry about the confusing rules for the _foo namespace.
  2164. !      But HPUX 7.0 uses _edata, so we might as weel be consistent.  */
  2165. !   symbol_define ("__edata", N_DATA | N_EXT, &edata_symbol_alt);
  2166. !   symbol_define ("__etext", N_TEXT | N_EXT, &etext_symbol_alt);
  2167. !   symbol_define ("__end", N_BSS | N_EXT, &end_symbol_alt);
  2168.   
  2169.   #ifdef sun
  2170.     {
  2171. !     symbol *dynamic_symbol;
  2172. !     symbol_define ("__DYNAMIC", N_ABS | N_EXT, &dynamic_symbol);
  2173. !     if (dynamic_symbol)
  2174. !       dynamic_symbol->value = 0;
  2175. !   }
  2176. ! #endif
  2177. ! #ifdef sequent
  2178. !   {
  2179. !     symbol *i387_flt_symbol;
  2180. !     symbol_define ("_387_flt", N_ABS | N_EXT, &i387_flt_symbol);
  2181. !     if (i387_flt_symbol)
  2182. !       i387_flt_symbol->value = 0;
  2183. !   }
  2184. ! #endif
  2185. ! #ifdef NeXT
  2186. !   {
  2187. !     symbol *shlib_init_symbol;
  2188. !     symbol_define ("__shared_library_initialization", N_UNDF | N_EXT, &shlib_init_symbol);
  2189. !     if (shlib_init_symbol)
  2190. !       shlib_init_symbol->max_common_size = sizeof (long int);
  2191.     }
  2192. ! #endif
  2193.   }
  2194.   
  2195.   /* Compute the hash code for symbol name KEY.  */
  2196.